package cn.xspace.wnx.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.xspace.android.xafcommon.base.BaseActivity;
import com.xspace.android.xafcommon.base.PagerInfoView;
import com.xspace.android.xafcommon.been.BaseReturnBean;
import com.xspace.android.xafcommon.util.Constants;
import com.xspace.android.xafcommon.util.EventBusConstant;
import com.xspace.android.xafcommon.util.HeadShowUtil;
import com.xspace.android.xafcommon.util.IntentConstant;
import com.xspace.android.xafcommon.util.T;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import cn.xspace.app.view.TitleBar;
import cn.xspace.library.bean.Type;
import cn.xspace.library.util.BldNavigation;
import cn.xspace.library.util.MsgType;
import cn.xspace.library.util.MyInfoUtil;
import cn.xspace.library.util.OkHttpManager;
import cn.xspace.library.util.Tools;
import cn.xspace.wnx.R;
import cn.xspace.wnx.entity.ServiceObjBean;
import cn.xspace.wnx.util.Navigator;
import de.greenrobot.event.EventBus;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

public class MyUserInfoActivity extends BaseActivity {

    private TitleBar mTitleBar;
    private RelativeLayout layoutUserInfo;
    //    private RelativeLayout rlNetWorkInfo;
//    private Button btReload;
    private PagerInfoView mPagerInfo;

    private RelativeLayout serviceObjType;
    private LinearLayout serviceObjOrg;
    private RelativeLayout mobile;

    private ImageView headImg;
    private ImageView sexImg;
    private TextView name;
    private TextView area;
    private TextView user_desc;
    private TextView phone;
    private TextView type;
    private TextView orgname;
    private TextView nj_username;
    private ServiceObjBean serviceObjBean;
    private RelativeLayout sendMsgRelative;

    private String user_id;
    private String friend_id;
    private String serviceobj_id;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    protected void getIntentData() {
        super.getIntentData();
        user_id = MyInfoUtil.getMyUserid(mContext);
        friend_id = getIntent().getStringExtra(IntentConstant.friend_id);
        serviceobj_id = getIntent().getStringExtra(IntentConstant.serviceobj_id);
    }

    @Override
    protected void initView() {
        setContentView(R.layout.activity_my_user_info);
    }

    @Override
    protected void initFindViewById() {
        super.initFindViewById();
        mTitleBar = (TitleBar) findViewById(R.id.object_info_title_bar);
        layoutUserInfo = (RelativeLayout) findViewById(R.id.layout_user_info);
        layoutUserInfo.setVisibility(View.INVISIBLE);
        mPagerInfo = (PagerInfoView) findViewById(R.id.service_obj_info_pager);
//        rlNetWorkInfo = (RelativeLayout) findViewById(R.id.layout_net_work_info);
//        btReload = (Button) findViewById(R.id.bt_net_work_reload);
        serviceObjType = (RelativeLayout) findViewById(R.id.service_obj_type);
        serviceObjOrg = (LinearLayout) findViewById(R.id.service_obj_org);//机构默认不显示,散户不显示,如果是机构则显示
        headImg = (ImageView) findViewById(R.id.img_head);
        sexImg = (ImageView) findViewById(R.id.img_sex);
        name = (TextView) findViewById(R.id.txt_user_name);
        area = (TextView) findViewById(R.id.txt_area);
        user_desc = (TextView) findViewById(R.id.txt_person_word);
        phone = (TextView) findViewById(R.id.txt_phone);
        type = (TextView) findViewById(R.id.txt_service_obj_type);
        orgname = (TextView) findViewById(R.id.txt_service_org_name);
        nj_username = (TextView) findViewById(R.id.txt_zpsy);
        mobile = (RelativeLayout) findViewById(R.id.rl_service_person_phone);
        sendMsgRelative = (RelativeLayout) findViewById(R.id.relativew_send_msg);
    }

    @Override
    protected void initData() {
        requestNetWorkData();
    }

    @Override
    protected void initEvent() {
        super.initEvent();
//        rlNetWorkInfo.setVisibility(View.GONE);
        mTitleBar.setLeftLayoutClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        serviceObjType.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(mContext, ServiceObjDetailActivity.class);
                intent.putExtra(IntentConstant.serviceobj_id, serviceobj_id);
                startActivity(intent);
            }
        });
        mobile.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (serviceObjBean != null && !TextUtils.isEmpty(serviceObjBean.user_mobile)) {
                    ArrayList<String> actionMode = new ArrayList<>();
                    actionMode.add(serviceObjBean.user_mobile);
                    actionMode.add(Tools.Action.CALL_PHONE);
                    actionMode.add(Tools.Action.SEND_MESSAGE);
                    actionMode.add(Tools.Action.CLIP_NUMBER);
                    actionMode.add(Tools.Action.CANCEL);
                    Tools.showPhoneOrEmailDialog(mContext, actionMode);
                }
            }
        });
        sendMsgRelative.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle dataBundle = new Bundle();
                dataBundle.putString(BldNavigation.SENDER_ID, serviceObjBean.user_id);
                dataBundle.putInt(BldNavigation.CHAT_TYPE, MsgType.CHAT_SINGLE.toNumber());
                dataBundle.putString(BldNavigation.CHAT_NAME, serviceObjBean.user_name);
                dataBundle.putString(BldNavigation.CHAT_SENDER_LOGO, serviceObjBean.user_logo);
                dataBundle.putString(BldNavigation.CHAT_SENDER_SEX, serviceObjBean.user_sex);
                Navigator.startChatActivity(mContext, dataBundle);
            }
        });

        mPagerInfo.setOnReloadClickListener(new PagerInfoView.OnReloadClickListener() {
            @Override
            public void onReloadClick(View v) {
                requestNetWorkData();
            }
        });
    }

    private void requestNetWorkData() {
        showWaitDialog();
        HashMap<String, Object> map = new HashMap<>();
        map.put("user_id", user_id);
        map.put("friend_id", friend_id);
        map.put("serviceobj_id", serviceobj_id);
        registerSubscription(OkHttpManager.getInstance().commonRequest(Constants.QUERY_SERVICE_OBJ_BASEINFO, map)
                .subscribeOn(Schedulers.io())
                .map(new Func1<BaseReturnBean, ServiceObjBean>() {
                    @Override
                    public ServiceObjBean call(BaseReturnBean baseReturnBean) {
                        return new Gson().fromJson(baseReturnBean.data, new
                                TypeToken<ServiceObjBean>() {
                                }.getType());
                    }
                })
                .observeOn(AndroidSchedulers.mainThread()).subscribe(new Subscriber<ServiceObjBean>() {
                    @Override
                    public void onCompleted() {
                    }

                    @Override
                    public void onError(Throwable e) {
                        hideWaitDialog();
                        e.printStackTrace();
                        dealNetWorkError();
                    }

                    @Override
                    public void onNext(ServiceObjBean serviceObjBaseInf) {
                        hideWaitDialog();
                        if (serviceObjBaseInf == null) {
                            T.show(mContext, getResources().getString(R.string.request_data_error), Toast.LENGTH_SHORT);
                            return;
                        }
                        serviceObjBean = serviceObjBaseInf;
                        setData(serviceObjBaseInf);
                    }
                }));
    }

    private void setData(ServiceObjBean serviceObjBean) {
        layoutUserInfo.setVisibility(View.VISIBLE);
        HeadShowUtil.headShow(mContext, serviceObjBean.user_logo, serviceObjBean.user_sex, headImg, HeadShowUtil
                .HeadType.UserLogo);
        if (!TextUtils.isEmpty(serviceObjBean.user_sex)) {
            if (serviceObjBean.user_sex.equals("男")) {
                sexImg.setImageResource(R.drawable.icon_man);
            } else if (serviceObjBean.user_sex.equals("女")) {
                sexImg.setImageResource(R.drawable.icon_woman);
            }
        }
        name.setText(serviceObjBean.user_name);
        area.setText(serviceObjBean.user_areaname);
        user_desc.setText(TextUtils.isEmpty(serviceObjBean.user_desc) ? getResources().getString(R.string
                .person_word_default) : serviceObjBean.user_desc);
        phone.setText(serviceObjBean.user_mobile);
        type.setText(serviceObjBean.object_typename);
        if (Type.PersonOrOrg.ORG.equals(serviceObjBean.user_type)) { //机构才显示机构信息
            serviceObjOrg.setVisibility(View.VISIBLE);
        }
        orgname.setText(serviceObjBean.factory_name);
        nj_username.setText(serviceObjBean.nj_username);
    }

    private void dealNetWorkError() {
        mPagerInfo.showNetWorkError();
    }

    @Override
    public void onEventMainThread(Map<String, Object> inParam) {
        String type = inParam.get(EventBusConstant.TYPE).toString();
        if (EventBusConstant.SERVICE_OBJECT_STOP_SUCCESS.equals(type)) {
            String serviceObjId = inParam.get(EventBusConstant.ACTION_KEY).toString();
            if (serviceObjId.equals(serviceobj_id)) {
                finish();
            }
        }
    }
}
